<HTML><HEAD>
<!--
    -------------
    Guessing Game
    -------------
-->

<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers

/*
    THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
    Copyright (c)1998 by Charles River Media.  All Rights Reserved.
    
    This applet can only be re-used or modifed by license holders of the
    JavaScript Cookbook CD-ROM.  Credit must be given in the source
    code and this copyright notice must be maintained. If you do
    not hold a license to the JavaScript Cookbook, you may NOT
    duplicate or modify this code for your own use.

    Use at your own risk. No warranty is given or implied of the suitability 
    of this applet for any specific application. Neither Erica Sadun nor 
    Charles River Media will be held responsible for any unwanted effects 
    due to the use of this applet or any derivative. 
*/


//------------------RANDOM NUMBERS----------------------------
var js_mult1=3141
var js_mult2=5821
var js_m1=100000000
var js_m2=10000
var js_iseed=0
var js_iseed1=0
var js_iseed2=0


// Return a Random Integer between 0 and N-1
function random(n)
{
    if (js_iseed == 0)
    {
        now = new Date()
        js_iseed = now.getHours() + now.getMinutes() * 60 
                    + now.getSeconds() * 3600
    }
    js_iseed1 = js_iseed / js_m2
    js_iseed2 = js_iseed % js_m2
    var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) * 
                js_m2 + (js_iseed2 * js_mult2)) % js_m1
    js_iseed = (tmp + 1) % js_m1
    return (Math.floor((js_iseed/js_m1) * n))
}

// Initialize number of guesses and the secret number

var myNumber = 0
var nGuesses = 0

// Process
function guessnum()
{
    var response
    var num = parseInt(document.forms[0].guess.value)
    
    document.forms[0].guess.value = num // visual feedback in case of NaN

    nGuesses++ // increase the guess count
    response = "Your number was "
    
    if (num < myNumber)    response = response + "too low"
    if (num > myNumber)    response = response + "too high"
    if (num == myNumber) 
    {
        response = response + "right!!  Guess my new number."
        nGuesses = 0
        myNumber = random(100)+1
    }
    document.forms[1].result.value=response
    document.forms[1].guesses.value=nGuesses
    document.forms[0].guess.focus()
    document.forms[0].guess.select()
    return true
}
    
function giveup()
{
    var response
    
    nGuesses = 0
    response = "The number was " + myNumber +".  Guess my new number."
    myNumber = random(100)+1
    document.forms[1].result.value=response
    document.forms[1].guesses.value=nGuesses
    document.forms[0].guess.focus()
    document.forms[0].guess.select()
}

<!-- done hiding --></SCRIPT></HEAD>

<BODY bgcolor="ffffff" link="0000ff" vlink="770077"
    onLoad="document.forms[0].guess.focus(); document.forms[0].guess.select()">
    
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/SPICE.JPG" WIDTH=37 HEIGHT=72
ALIGN = LEFT>Guessing Game</H1></FONT>

<BLOCKQUOTE>
    <FONT SIZE=4>
        This script randomly chooses a number between 
        one and a hundred.  The player guesses at the
        number and is given hints.<p>
    </FONT>        

    <FONT COLOR="770000">
    I am thinking of a number from 1 to 100.  Can you guess what it is?
    </FONT>
</BLOCKQUOTE>
    
<SCRIPT>
    myNumber = random(100)+1
    nGuesses = 0
</SCRIPT>
    
<FORM onSubmit="guessnum(); return false">
Your Guess: <INPUT TYPE="text" NAME="guess" SIZE=5>
<INPUT TYPE="button" VALUE="     Guess...   " onClick="guessnum()">
<INPUT TYPE="button" VALUE="  GiveUp  " onClick="giveup()">
</FORM>

<FORM><PRE>
#-Guesses: <INPUT TYPE="text" NAME="guesses" SIZE=3>
     INFO: <INPUT TYPE="text" NAME="result" SIZE=44>
</PRE></FORM>
    
<BR><BR>
    
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
A player can guess or give up by tapping on the two buttons 
to the right of the 
guess box.  These buttons use the 
<FONT COLOR="770000">onClick</FONT> event to check
to call the guess and give-up functions.<p>

The script sets the focus to the input field when the
page loads by including 
<PRE><FONT COLOR="770000" SIZE=3>    onLoad="document.forms[0].guess.focus()"
</FONT></PRE>
</blockquote> in the initial body tag.  This means that the
game can be played right away without clicking on the "guess"
text input field.<p>

<b>Note:</b> Because a player might enter text rather than a number,
<FONT color=770000">guess</FONT> updates the guess box
to a parseInt() function call.  If a player typed an invalid
text string, rather than a number, the guess box shows "NaN".
Netscape treats this non-number as a zero, more or less.
(Netscape Navigator version 3.0 will permit scripts to test for "NaN")
</FONT>
    
<FONT color="770000" SIZE=3><PRE>
    var num = parseInt(thisform.guess.value)
    thisform.guess.value = num
</pre></font>

<h5>Copyright &copy;1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>